﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.OleDb;

public partial class add_reservation : System.Web.UI.Page
{

    OleDbConnection conx;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Calendar2.SelectedDate = Calendar1.SelectedDate = DateTime.Now;
        }

        //Label1.Text = Calendar1.SelectedDate.ToString("dd-MMM-yyyy");
        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            Label2.Visible = true;
            Label3.Visible = true;
        }
        else
        {
            Label2.Visible = false;
            Label3.Visible = false;
        }
    }

    protected void Button_Reservation_Click(object sender, EventArgs e)
    {

        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            Label1.Visible = true;
            Label1.Text = "Please Select Check in and Check out Date<br />(Check in before Check out) <br/>";
            Label2.Visible = true;
            Label3.Visible = true;
            return;
        }
        else
        {
            Label2.Visible = false;
            Label3.Visible = false;
        }

        try
        {
            String con = "Provider=MSDAORA;Data Source=orcl;Password=oracle;User ID=pearl";//"DSN=orcl;Driver={Microsoft ODBC for Oracle};UID=system;PWD=password;SERVER=localhost;";
            conx = new OleDbConnection(con);
            conx.Open();

            if (conx.State == ConnectionState.Open)
            {
                Label1.Text = "Oracle Connected";

                string ddl1 = DropDownList1.SelectedValue;
                string ddl2 = DropDownList2.SelectedValue;
                string ddl3 = DropDownList3.SelectedValue;
                string cal_start = Calendar1.SelectedDate.ToString("dd-MMM-yyyy");
                string cal_end = Calendar1.SelectedDate.ToString("dd-MMM-yyyy");
                string ddl4 = DropDownList4.SelectedValue;

                DataSet ds = new DataSet("Dataset1");

                OleDbDataAdapter ODDA = new OleDbDataAdapter("SELECT AMOUNT FROM COUNTS WHERE NAME = 'RESERVATION'", conx);
                ODDA.Fill(ds, "MYDATA");

                string test = ds.Tables["MYDATA"].Rows[0]["AMOUNT"].ToString();
                //Label2.Text = test;
                int reservation_amt = Convert.ToInt32(test);


                OleDbCommand ODC = new OleDbCommand("INSERT INTO RESERVATIONS VALUES (" + reservation_amt + ",'" + ddl1 + "','" + ddl2 + "','" + ddl3 + "','" + cal_start + "','" + cal_end + "','" + ddl4 + "')", conx);
                ODC.ExecuteNonQuery();
                OleDbCommand ODC_commit = new OleDbCommand("COMMIT", conx);
                ODC_commit.ExecuteNonQuery();

                reservation_amt = reservation_amt + 1;

                OleDbCommand ODC_update = new OleDbCommand("UPDATE COUNTS SET AMOUNT = " + reservation_amt + " WHERE NAME = 'RESERVATION'", conx);
                ODC_update.ExecuteNonQuery();

                ODC_update = new OleDbCommand("UPDATE ROOMS SET STATUS = '" + "Y" +"' WHERE ROOM_NO = '" + DropDownList2.Text + "'", conx);
                ODC_update.ExecuteNonQuery();
                ODC_update = new OleDbCommand("COMMIT", conx);
                ODC_update.ExecuteNonQuery();

                Label1.Text = "\"New Reservation Inserted and Room NO. " + DropDownList2.Text+ " are updated.\"";
            }
            else
                Label1.Text = "Oracle Connect Failed";

        }

        catch (Exception ex)
        {
            Label1.Text = ex.Message + "<br />" + ex.StackTrace;
        }

        finally
        {
            conx.Close();
            conx = null;
        }
    }
    
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            Label2.Visible = true;
            Label3.Visible = true;
        }
        else
        {
            Label2.Visible = false;
            Label3.Visible = false;
        }
    }
    protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    {
        if (DateTime.Compare(Calendar1.SelectedDate, Calendar2.SelectedDate) >= 0)
        {
            Label2.Visible = true;
            Label3.Visible = true;
        }
        else
        {
            Label2.Visible = false;
            Label3.Visible = false;
        }
    }
    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("reservation.aspx");
    }
}